#!/bin/bash
# description：默认路径为/usr/local，若要变更则修改MYSQL_HOME
# mysql包为：mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz
# 使用该脚本时，最好使用上述mysql5.7.26 二进制安装包，也可安装该脚本逻辑，自己需要适配相关版本的安装步骤
MYSQL_HOME='/usr/local'

#my-default.cnf参数检查
checkCNF(){
	BASEDIR=$(cat $MYSQL_HOME/mysql/support-files/my-default.cnf |grep -v "#"|grep basedir)
	if [[ $(echo $BASEDIR|awk -F '=' '{print $2}')  != $MYSQL_HOME/mysql ]];then
        	sed -i "s|$BASEDIR|basedir = $MYSQL_HOME/mysql|g" $MYSQL_HOME/mysql/support-files/my-default.cnf
	fi
	DATADIR=$(cat $MYSQL_HOME/mysql/support-files/my-default.cnf |grep -v "#"|grep datadir)
	if [[ $(echo $DATADIR|awk -F '=' '{print $2}') != $MYSQL_HOME/mysql/data ]];then
        	sed -i "s|$DATADIR|datadir = $MYSQL_HOME/mysql/data|g" $MYSQL_HOME/mysql/support-files/my-default.cnf
	fi
	LOGDIR=$(cat $MYSQL_HOME/mysql/support-files/my-default.cnf |grep -v "#"|grep log)
    if [[ $(echo $LOGDIR|awk -F '=' '{print $2}') != $MYSQL_HOME/mysql/log/mysqld.log ]];then
            sed -i "s|$LOGDIR|log-error = $MYSQL_HOME/mysql/log/mysqld.log|g" $MYSQL_HOME/mysql/support-files/my-default.cnf
    fi
	#检查pid-file = $MYSQL_HOME/mysql/mysqld.pid
	PIDDIR=$(cat $MYSQL_HOME/mysql/support-files/my-default.cnf |grep -v "#"|grep pid)
    if [[ $(echo $PIDDIR|awk -F '=' '{print $2}') != $MYSQL_HOME/mysql/mysqld.pid ]];then
             sed -i "s|$PIDDIR|pid-file = $MYSQL_HOME/mysql/mysqld.pid|g" $MYSQL_HOME/mysql/support-files/my-default.cnf
    fi
	#检查socket = $MYSQL_HOME/mysql/mysql.sock
	SOCKETLINENUM=$(cat -n $MYSQL_HOME/mysql/support-files/my-default.cnf |grep -v "#"|grep socket|wc -l)
	SOCKETLINE=$(cat -n $MYSQL_HOME/mysql/support-files/my-default.cnf |grep -v "#"|grep socket|awk '{print $1}')
	for((i=1;i<=${SOCKETLINENUM};i++));
	do
        LINE=$(echo $SOCKETLINE|awk '{print $'$i'}')
        SOCKDIR=$(sed -n  ${LINE}p /usr/local/mysql/support-files/my-default.cnf)
        if [[ $(echo $SOCKDIR|awk -F '=' '{print $2}') != $MYSQL_HOME/mysql/mysql.sock ]];then
             sed -i "s|$SOCKDIR|socket = $MYSQL_HOME/mysql/mysql.sock|g" $MYSQL_HOME/mysql/support-files/my-default.cnf
        fi
	done
}
#检查其他库文件并删除
if [[ -n $(rpm -qa | grep mysql) ]];then
	rpm -qa | grep mysql|awk '{print $0}'|xargs rpm -e --nodeps
fi
#检查mysql组
if [[ -z $(cat /etc/group|grep mysql) ]];then
	groupadd mysql
fi
#检查mysql用户
if [[ -z $(cat /etc/passwd | grep mysql) ]];then
	useradd -r -g mysql mysql
fi
id mysql
CHECK=$(ls $MYSQL_HOME |grep mysql)
if [[ -z $CHECK ]];then
	
	#解压Mysql,配置相关目录
	TARPATH=$(find . -name 'mysql*linux*.tar.gz')
	tar  -zvxf $TARPATH -C $MYSQL_HOME
    mv $MYSQL_HOME/$(ls $MYSQL_HOME |grep mysql) $MYSQL_HOME/mysql
	mkdir -p $MYSQL_HOME/mysql/data $MYSQL_HOME/mysql/log
	touch  $MYSQL_HOME/mysql/log/mysqld.log
	touch  $MYSQL_HOME/mysql/mysqld.pid
	touch  $MYSQL_HOME/mysql/mysql.sock
	
	#复制my.cnf
	CNFPATH=$(find . -name 'my-default.cnf')
	cp $CNFPATH $MYSQL_HOME/mysql/support-files
	
	#参数方法检查
	checkCNF
	
	#复制到etc
	cp $(find $MYSQL_HOME/mysql/ -name "my-default.cnf") /etc/my.cnf
	cp $(find $MYSQL_HOME/mysql/ -name "mysql.server") /etc/init.d/mysqld
	chown -R mysql:mysql $MYSQL_HOME/mysql/
	
	#配置Mysql环境变量
	echo "export PATH=$MYSQL_HOME/mysql/bin:\$PATH" >> /etc/profile 

	#执行安装初始化
	$MYSQL_HOME/mysql/bin/mysqld_safe --initialize --user=mysql --basedir=$MYSQL_HOME/mysql --datadir=$MYSQL_HOME/mysql/data

	#开始SSL 
	$MYSQL_HOME/mysql/bin/mysql_ssl_rsa_setup
	#配置Mysql自启动
	chkconfig --add mysqld
	
	#提取临时密码 
	cat $MYSQL_HOME/mysql/log/mysqld.log |grep "temporary password"|awk -F'for' '{print $2}' > passwd.txt

    #启动Mysql5.7
	/etc/init.d/mysqld start 
fi
ps -ef |grep mysql|grep -v grep
echo "mysql installed.."
echo "temporary password: " $(cat $(find . -name 'passwd.txt')) 
sleep 1s 
exit 0